<?php

class Product extends Controller
{
	// Protected or private properties
	protected $_template;
	
	// Constructor
	public function __construct()
	{
		parent::Controller();
		// load language to initialize	
		//$this->session->unset_userdata('lang');
		/*
		$lang = $this->session->userdata('lang');
		echo $this->session->userdata('ip_address');
		var_dump($lang); echo '<br/>'; */
		//$this->output->cache(3600);
		$this->template['module'] = 'product';
		$this->template['lang_file'] = 'product';

		// Load needed models, libraries, helpers and language files
		//echo "kkk"; exit();
		$this->load->module_model('product', 'product_model', 'product');
		$this->load->module_model('product', 'category_model', 'category');
		//$this->load->module_model('news', 'user_model', 'user');
		$this->load->module_model('news', 'language_model', 'language');
		
		$this->load->library('user_agent');
		//echo $lang['name'];
		/*
		if($lang == false)
		{
			//echo 'kkk';
			$language = $this->language->get_lang_id('vn');
			//var_dump($language);
			$lang = $language;
			$this->session->set_userdata('lang', $language);
		}*/
		//echo $lang['name'];
		//$this->load->module_language('news', 'general',$lang['name']);
		//$this->load->module_language('news', 'sidebar',$lang['name']);
		//$this->load->module_language('news', 'general');
		//$this->load->module_language('news', 'sidebar');
		//$this->load->library('securimage_library');		
	}

	// Public methodse
	public function filterprice($from,$to)
	{
		$this->load_language();		
		
		if ($data['posts'] = $this->product->filterprice($from,$to))
		{	
			$this->_template['page']	= 'product/search';
		}
		else
		{
			$this->_template['page']	= 'errors/search_no_results';
		}
			
		$this->system_library->load($this->_template['page'], $data);
		
	}
	public function index($page = null)
	{
		// Load language
		$lang = $this->load_language();
		$this->load->library('pagination');
		
		$this->load->module_language('product', 'general',$lang['name']);
		$this->load->module_language('product', 'sidebar',$lang['name']);
		$this->load->module_language('product', 'footer',$lang['name']);
		
		$config['total_rows'] = $this->product->get_posts_count($lang['id']);
		$config['per_page'] = 5;//$this->product->get_posts_per_page();
		$config['base_url'] = base_url().'product/index/';
    	$config['full_tag_open'] = '<p>';
    	$config['full_tag_close'] = '</p>';
    	$config['first_link'] = 'Đầu tiên';
    	$config['last_link'] = 'Cuối cùng';
		$this->pagination->initialize($config);
			
		$pages_count = ceil($config['total_rows'] / $config['per_page']);
		$page = ($page == 0) ? 1 : $page;
		$offset = $config['per_page'] * ($page - 1);
		
		if ($data['posts'] = $this->product->get_posts($config['per_page'], $offset,$lang['id']))
		{
			if ($page > $pages_count)
			{
				redirect('product', 'refresh');
			}
		
			$data['posts_per_page'] = 5;//$this->product->get_posts_per_page();
			$data['posts_count'] = $this->product->get_posts_count();
			$data['pages_count'] = $pages_count;
			$data['current_page'] = $page;
			$data['next_page'] = $page + 1;
			$data['previous_page'] = $page - 1;

			foreach ($data['posts'] as $key => $post)
			{
				$data['posts'][$key]['url'] = post_url($post['url_title'], $post['created_date']);
				$data['posts'][$key]['display_name'] = 'mike';//$this->users->get_user_display_name($post['author']);
			}

			$this->_template['page']	= 'product/home';
		}
		else
		{
			$this->_template['page']	= 'errors/no_posts';
		}
		
		$this->system_library->load($this->_template['page'], $data);
	}
	public function detail($id)
	{
		$this->load_language();			
		if($data['post'] = $this->product->get_product($id))
		{
			$relate_products = $this->product->get_relate_product($id);
			$data['relate_products'] = $relate_products;
			$this->_template['page']	= 'product/detail';
		}else
		{
			$this->_template['page']	= 'errors/404';
		}	
		$this->system_library->load($this->_template['page'], $data);
	}
	public function post($year = NULL, $month = NULL, $day = NULL, $url_title = NULL)
	{
		// Load language
		$this->load_language();
		
		$this->load->module_model('news', 'user_model', 'user');
			
		if ($data['post'] = $this->news->get_post_by_url($year, $month, $day, $url_title))
		{
			
			$data['post']['url'] = post_url($data['post']['url_title'], $data['post']['created_date']);
			$this->_template['page']	= 'news/single_post';
		}
		else
		{
			$this->_template['page']	= 'errors/404';
		}
			
		$this->system_library->load($this->_template['page'], $data);
	}
	public function category($id = null)
	{		
		// Load language
		$this->load_language();		
		//echo $url_name; exit();
		//echo $id; exit();
		$category = $this->category->get_category($id);
		$data['category'] = $category;
		if ($data['posts'] = $this->product->get_product_by_category($id))
		{
			foreach ($data['posts'] as $key => $post)
			{
				//var_dump($post); exit();
				$data['posts'][$key]['url'] = product_url($post['title'], $post['id']);
				$data['posts'][$key]['display_name'] = 'mike';//$this->users->get_user_display_name($post['author']);
			}

			$this->_template['page']	= 'product/category';
		}
		else
		{
			$this->_template['page']	= 'errors/404';
		}
			
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function search()
	{
		// Load language
		$this->load_language();
		
		$data['search_term'] = $this->input->post('keyword', TRUE);
		if ($data['search_term'] != " Keyword / Item #")
		{
			if ($data['posts'] = $this->product->search_by_term($data['search_term']))
			{	
				$this->_template['page']	= 'product/search';
			}
			else
			{
				$this->_template['page']	= 'errors/search_no_results';
			}
				
			$this->system_library->load($this->_template['page'], $data);
		}
		else
		{
			$this->_template['page']	= 'product/search';
			$this->system_library->load($this->_template['page'], $data);
		}
	}

	public function lang($lang = null)
	{
		if($lang == null)
		{
			return $lang = 'vn';
		}
		$language = $this->language->get_lang_id($lang);
		//print_r($language);
		$this->session->set_userdata('lang', $language);
		//redirect('', 'refresh');
		//for test session
		//$t = $this->session->userdata('lang');
		//print_r($t); //exit();
		//echo '<br/>';
		
		$this->load->library('pagination');
		$lang = $this->session->userdata('lang');
		
		$this->load->module_language('news', 'general',$lang['name']);
		$this->load->module_language('news', 'sidebar',$lang['name']);
		
		$config['total_rows'] = $this->news->get_posts_count($lang['id']);
		$config['per_page'] = $this->news->get_posts_per_page();
		$this->pagination->initialize($config);
			
		$pages_count = ceil($config['total_rows'] / $config['per_page']);
		$page = ($page == 0) ? 1 : $page;
		$offset = $config['per_page'] * ($page - 1);
			
		if ($data['posts'] = $this->news->get_posts($config['per_page'], $offset,$lang['id']))
		{
			if ($page > $pages_count)
			{
				redirect('news', 'refresh');
			}
		
			$data['posts_per_page'] = $this->news->get_posts_per_page();
			$data['posts_count'] = $this->news->get_posts_count();
			$data['pages_count'] = $pages_count;
			$data['current_page'] = $page;
			$data['next_page'] = $page + 1;
			$data['previous_page'] = $page - 1;

			foreach ($data['posts'] as $key => $post)
			{
				$data['posts'][$key]['url'] = post_url($post['url_title'], $post['created_date']);
				$data['posts'][$key]['display_name'] = 'mike';//$this->users->get_user_display_name($post['author']);
			}

			$this->_template['page']	= 'news/posts';
		}
		else
		{
			$this->_template['page']	= 'errors/no_posts';
		}
		
		$this->system_library->load($this->_template['page'], $data);
	}
	/*
	 * private function to load language
	 */
	public function load_language()
	{

		$lang = $this->session->userdata('lang');
		
		//load language
		if($lang == false)
		{
			$language = $this->language->get_lang_id('vn');
			$lang = $language;
			$this->session->set_userdata('lang', $language);
		}
		//var_dump(strtolower($lang['name']));
		$this->load->module_language('product', 'general',$lang['name']);
		//$this->load->module_language('news', 'sidebar',$lang['name']);
		//return $lang;
	}
}

/* End of file blog.php */
/* Location: ./application/modules/blog/controllers/blog.php */